package cn.hwadee.muying.service;

import cn.hwadee.muying.entity.Contract;
import cn.hwadee.muying.entity.Receipt;
import cn.hwadee.muying.mapper.ReceiptMapper;
import cn.hwadee.muying.commons.base.BaseService;
import cn.hwadee.muying.mapper.UserMapper;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * <p>
 * 收款情况 服务实现类
 * </p>
 *
 * @author Mankind
 * @since 2018-08-06
 */
@Service
public class ReceiptService extends BaseService<ReceiptMapper, Receipt> {

    @Autowired
    private ReceiptMapper receiptMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ContractService contractService;

    public MyPage<Receipt> list(MyPage<Receipt> page, User cUser,Integer finalPayment,String keyword) throws Exception{
        List<Receipt> receipts = receiptMapper.list(page,cUser,finalPayment,keyword);

        for(Receipt receipt:receipts){
            User sellerName = userService.selectById(receipt.getSellerId());
            receipt.setSellerName(sellerName.getUsername());

            if(receipt.getContractId()!=null){
                Contract contract=contractService.selectById(receipt.getContractId());
                if(contract.getUserId()!=null){
                    User client=userService.selectById(contract.getUserId());
                    receipt.setClientName(client.getUsername());
                }
            }
        }
        page.setRecords(receipts);
        return page;
    }

}
